{% extends 'layout.html' %}

{% block content %}
<div class="container">

    <div class="panel panel-default">
        <div class="panel-heading">任务表单</div>
        <div class="panel-body">
            <div class="panel-body">
                <form id="addForm" novalidate>
                    <div class="clearfix" >
                        {% for field in form %}
                            <div class="col-xs-6" >
                                <div class="form-group" style="position: relative;margin-bottom: 20px;" > 
                                    <!-- position: relative; 相对定位    -->
                                    <label>{{ field.label }}</label>
                                        {{ field }}   
                                        <span class="error-msg" style="color: red; position: absolute;"></span> 
                                        <!-- position: absolute; 绝对定位 -->
                                </div>
                            </div>
                        {% endfor %}  
                    </div>
                    <button type="button" id="btnAdd" class="btn btn-primary">保 存</button>
                </form>
            </div>
        </div>
      </div>

      <div class="panel panel-default">     
        <div class="panel-heading">
            <span class="glyphicon glyphicon-list" aria-hidden="true"></span>
            任务表单
        </div>
  
        <!-- Table -->
        <table class="table table-bordered">
          <thead>
            <tr>
              <th>ID</th>
              <th>标题</th>
              <th>级别</th>
              <th>详细信息</th>
              <th>负责人</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            {%for obj in queryset %}
            <tr>
              <th scope="row">{{ obj.id }}</th>
              <td>{{ obj.title }}</td>
              <td>{{ obj.get_level_display }}</td>
              <td>{{ obj.detail }}</td>
              <td>{{ obj.user.username }}</td>
              <td>
                <a class="btn btn-primary btn-xs" href="#" >编辑</a> 
                <a class="btn btn-danger btn-xs" href="#">删除</a>
            </td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
    <ul class="pagination">
        {{ page_string }}
    </ul>
    <span><a style="color: red;">{{ error }}</a></span>

</div>

{% endblock %}


{% block js %}
    <script type="text/javascript">

        $(function () {
            // 
            BtnAddEvent();
        })

        function BtnAddEvent() {
            $("#btnAdd").click(function () {
                //在点击之前清空 span的框的值
                $(".error-msg").empty();

                $.ajax({
                url:'/task/add/',
                type:"post", 
                data:$("#addForm").serialize(),
                dataType:"JSON",
                success:function(res){
                    if (res.status) {
                        alert("添加成功");
                        location.reload();
                    }else{
                            //console.log(res.error);
                        //循环
                        $.each(res.error,function (name,data) {
                            //console.log(name , data);
                            $("#id_" + name).next().text(data[0]);
                        })
                    }
                }
            })
            }

            ) 
        }

   </script>
{% endblock %}
